#include "Price.h"


namespace TL {
    Price Price::ExtractPrice(const char *str)
    {
        char temp_buffer[32];
        int i, j;
        bool comma = false, digits = false, unary = false;
        
        for (i = 0, j = 0; str[i]; i++) {
            if (!unary && !digits && (str[i] == '-' || str[i] == '+')) {
                temp_buffer[j++] = str[i];
                unary = true;
            }
            else if (!comma && digits && (str[i] == '.' || str[i] == ',')) {
                temp_buffer[j++] = '.';
                comma = true;
            }
            else if (!digits && isdigit(str[i])) {
                temp_buffer[j++] = str[i];
                digits = true;
                unary = true;
            }
            else if (digits && isdigit(str[i])) {
                temp_buffer[j++] = str[i];
            }
        }

        temp_buffer[j] = '\0';
        
        return Price(temp_buffer);
    }
    Price Price::ExtractPrice(const String &str)
    {
        char temp_buffer[32];
        int i, j;
        bool comma = false, digits = false, unary = false;

        for (i = 0, j = 0; str[i]; i++) {
            if (!unary && !digits && (str[i] == '-' || str[i] == '+')) {
                temp_buffer[j++] = str[i];
                unary = true;
            }
            else if (!comma && digits && (str[i] == '.' || str[i] == ',')) {
                temp_buffer[j++] = '.';
                comma = true;
            }
            else if (!digits && isdigit(str[i])) {
                temp_buffer[j++] = str[i];
                digits = true;
                unary = true;
            }
            else if (digits && isdigit(str[i])) {
                temp_buffer[j++] = str[i];
            }
        }

        temp_buffer[j] = '\0';

        return Price(temp_buffer);
    }

    Price::Price()
    {
        m_Number = 0.0;
        m_String = "0.0";
    }
    Price::Price(double num)
    {
        m_Number = num;

        char temp_str[64];
        sprintf(temp_str, "%.2f", num);
        m_String = temp_str;
    }
    Price::Price(String str)
    {
        m_Number = atof(str.GetCharString());
        m_String = str;
    }
    Price::Price(const Price &price)
    {
        m_Number = price.m_Number;
        m_String = price.m_String;
    }
    Price::~Price()
    {
    }

    double Price::GetNumber() const
    {
        return m_Number;
    }
    String Price::GetString() const
    {
        return m_String;
    }
    void Price::Set(double num)
    {
        m_Number = num;

        char temp_str[64];
        sprintf(temp_str, "%.2f", num);
        m_String = temp_str;
    }
    void Price::Set(String str)
    {
        m_Number = atof(str.GetCharString());
        m_String = str;
    }

    Price &Price::operator=(double num)
    {
        m_Number = num;

        char temp_str[64];
        sprintf(temp_str, "%.2f", num);
        m_String = temp_str;

        return *this;
    }
    Price &Price::operator=(String str)
    {
        m_Number = atof(str.GetCharString());
        m_String = str;

        return *this;
    }
    Price &Price::operator=(const Price &price)
    {
        m_Number = price.m_Number;
        m_String = price.m_String;

        return *this;
    }
}
